package studentsinfomanagement.view;

import studentsinfomanagement.bean.Administrator;
import studentsinfomanagement.bean.Student;
import studentsinfomanagement.bean.Teacher;
import studentsinfomanagement.main.Main;
import studentsinfomanagement.view.administrator.AdministratorHomeView;
import studentsinfomanagement.view.student.StudentHomeView;
import studentsinfomanagement.view.teacher.TeacherHomeView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Enumeration;

/**
 * 登录界面类

 */
public class LoginView extends JFrame {

    LoginView loginView = null;
    Main main = null;//逻辑层对象

    public LoginView(Main main){
        loginView = this;
        this.main = main;
        this.setTitle("学生信息管理系统");//标题
        this.setSize(350,230);//界面大小
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭界面退出程序
        this.setLocationRelativeTo(null);//界面默认居中
        this.setResizable(false);//界面大小不可调节
        FlowLayout fl = new FlowLayout();//流式布局
        this.setLayout(fl);

        JLabel jl_acc = new JLabel("账号");
        JLabel jl_pwd = new JLabel("密码");

        JTextField jt_acc = new JTextField();//账号输入框
        jt_acc.setPreferredSize(new Dimension(280,30));
        JPasswordField jp_pwd = new JPasswordField();//密码输入框
        jp_pwd.setPreferredSize(new Dimension(280,30));

        ButtonGroup btn_grp = new ButtonGroup();
        JRadioButton jr_teacher = new JRadioButton("老师");
        JRadioButton jr_student = new JRadioButton("学生");
        JRadioButton jr_administrator = new JRadioButton("管理员");
        btn_grp.add(jr_teacher);
        btn_grp.add(jr_student);
        btn_grp.add(jr_administrator);

        JButton login_btn = new JButton("登录");//登录按钮
        login_btn.setPreferredSize(new Dimension(250,25));
        JButton register_btn = new JButton("注册");//注册按钮
        register_btn.setPreferredSize(new Dimension(250,25));

        this.add(jl_acc);
        this.add(jt_acc);
        this.add(jl_pwd);
        this.add(jp_pwd);
        this.add(jr_teacher);
        this.add(jr_student);
        this.add(jr_administrator);
        this.add(login_btn);
        this.add(register_btn);

        this.setVisible(true);//设置界面可见

        //为按钮添加鼠标监听器
        //登录按钮
        login_btn.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //获取输入的账号密码身份
                String acc = jt_acc.getText().trim();
                String pwd = new String(jp_pwd.getPassword()).trim();
                String s_id = null;
                Enumeration<AbstractButton> radioBtns=btn_grp.getElements();
                while(radioBtns.hasMoreElements()){
                    AbstractButton btn = radioBtns.nextElement();
                    if(btn.isSelected()){
                        s_id = btn.getText();
                        break;
                    }
                }
                //2.判断信息是否完整
                if(acc.equals("") || pwd.equals("")  || s_id == null){
                    JOptionPane.showMessageDialog(loginView,"请输入完整！");
                }else{
                    int type;
                    if(s_id.equals("学生")){
                        type= 0;
                    }else if(s_id.equals("老师")){
                        type = 1;
                    }else{//管理员
                        type = 2;
                    }
                    //调用逻辑层的登录方法
                    boolean result = main.loginSystem(type,acc,pwd);
                    if(!result){//登录失败
                        JOptionPane.showMessageDialog(loginView,"登录失败，请检查您的账号和密码！");
                    }else{
                        loginView.setVisible(false);
                        if(type == 0) {//登录成功，进入学生个人主页
                            Student student = new Student();
                            student.setSno(acc);//携带学生学号参数
                            new StudentHomeView(main, student);
                        }else if(type == 1){//登录成功，进入老师个人主页
                            Teacher teacher = new Teacher();
                            teacher.setTno(acc);
                            new TeacherHomeView(main,teacher);
                        }else{//管理员登录成功，进入管理员个人主页
                            Administrator administrator = new Administrator();
                            administrator.setAccNum(acc);
                            new AdministratorHomeView(administrator);
                        }
                    }
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        //注册按钮
        register_btn.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new RegisterView(main);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }
}
